/**
 * 商品报溢管理初始化
 */
var GoodsOverflow = {
    id: "GoodsOverflowTable",	//表格id
    overflowData:{},
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
GoodsOverflow.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '报溢单号', field: 'overflowOrderNumber', visible: true, align: 'center', valign: 'middle'},
        {title: '创建者', field: 'createBy', visible: true, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: true, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: true, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: true, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 初始化表格的列
 */
GoodsOverflow.initGoodsOverflowColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键', field: 'goodsId', visible: false, align: 'center', valign: 'middle'},
        {title: '商品编码', field: 'goodsCode', visible: true, align: 'center', valign: 'middle'},
        {title: '商品型号', field: 'goodsModel', visible: true, align: 'center', valign: 'middle'},
        {title: '商品名称', field: 'goodsName', visible: true, align: 'center', valign: 'middle'},
        {title: '报损数量', field: 'goodsNumber', visible: true, align: 'center', valign: 'middle'},
        {title: '报损价格', field: 'goodsPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '报损总价', field: 'totalPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '商品单位', field: 'goodsUnit', visible: true, align: 'center', valign: 'middle'},
        {title: '创建者', field: 'createBy', visible: false, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: false, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: false, align: 'center', valign: 'middle'},
        {title: '商品报溢主键', field: 'goodsOverflowId', visible: false, align: 'center', valign: 'middle'},
        {title: '商品id', field: 'goodsId', visible: false, align: 'center', valign: 'middle'}
    ];
};


/**
 * 检查是否选中
 */
GoodsOverflow.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        GoodsOverflow.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加商品报溢
 */
GoodsOverflow.openAddGoodsOverflow = function () {
    var index = layer.open({
        type: 2,
        title: '添加商品报溢',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/goodsOverflow/goodsOverflow_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看商品报溢详情
 */
GoodsOverflow.openGoodsOverflowDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '商品报溢详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/goodsOverflow/goodsOverflow_update/' + GoodsOverflow.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 选中商品后计算出价格并赋值
 */
GoodsOverflow.calculateTotalPrice = function (cTotalPrice) {
    var cAmountPaid = $("#amountPaid").val();
    if (cAmountPaid === '') {
        $("#amountPaid").val(cTotalPrice);
    } else {
        var amountPaid = parseFloat(cAmountPaid);
        //累加价格
        $("#amountPaid").val(cTotalPrice + amountPaid);
    }
}

/**
 * 删除商品报溢
 */
GoodsOverflow.delete = function () {
    if (this.check()) {
        var selected = this.table.btInstance.bootstrapTable('getSelections');
        console.log(selected[0].goodsId);
        this.table.btInstance.bootstrapTable('removeByUniqueId', selected[0].goodsId);
    }
};

/**
 * 查询商品报溢列表
 */
GoodsOverflow.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    GoodsOverflow.table.refresh({query: queryData});
};


/**
 * 保存前检查数据
 */
GoodsOverflow.checkData = function () {
    var data = this.table.btInstance.bootstrapTable('getData', true);

    if ('' === data || typeof data === 'undefined' || data.length === 0) {
        Feng.info('请先添加商品!');
        return false;
    }

    var amountPaid = $("#amountPaid").val();
    if ('' === amountPaid || typeof amountPaid === 'undefined') {
        Feng.info('请输入实付金额!');
        return false;
    }

    var createDate = $("#createDate").val();
    if('' === createDate || typeof createDate === 'undefined'){
        Feng.info("请选择报溢日期!");
        return false;
    }

    var overflowOrderNumber = $("#overflowOrderNumber").text();
    if ('' === overflowOrderNumber || typeof overflowOrderNumber === 'undefined') {
        Feng.info("订单号为空，请刷新页面重新提交!");
        return false;
    }

    return true;
}


/**
 * 清除数据
 */
GoodsOverflow.clearData = function () {
    this.overflowData = {};
}

/**
 * 收集数据
 */
GoodsOverflow.collectData = function () {
    this
        .set('id')
        .set('amountPaid')
        .set('createDate')
        .set('remarks')
}

/**
 * 清除dom显示数据
 */
GoodsOverflow.clearDom = function () {
    this
        .setDomVal('id')
        .setDomVal('amountPaid')
        .setDomVal('remarks')
        .setDomVal('createDate')
}


/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsOverflow.set = function (key, val) {
    this.overflowData[key] = (typeof val === "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置dom中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsOverflow.setDomVal = function (key, val) {
    (typeof val === "undefined") ? $("#" + key).val('') : $("#" + key).val(val);
    return this;
}

/**
 * 保存销售订单
 */
GoodsOverflow.save = function(){
    if (this.checkData()) {
        //清除数据
        this.clearData();
        //获取form数据
        this.collectData();
        var ajax = new $ax(Feng.ctxPath + "/goodsOverflow/add", function (data) {
            Feng.success(data.message);
            //清除表单
            GoodsOverflow.clearData();
            //清除dom数据显示
            GoodsOverflow.clearDom();
            //清除表格数据
            GoodsOverflow.table.btInstance.bootstrapTable('removeAll');
            //输出
            console.log('报损单号：', data.result);
            //重新赋值单号
            $("#overflowOrderNumber").text(data.result);
        }, function (data) {
            Feng.error("获取供应商失败!" + data.responseJSON.message + "!");
        });
        //获取单据号码
        this.overflowData['overflowOrderNumber'] = $('#overflowOrderNumber').text();
        //获取添加的表格数据
        this.overflowData['goodsOverflowList'] = this.table.btInstance.bootstrapTable('getData', true);
        //请求数据
        var requestData = JSON.stringify(this.overflowData);
        console.log("请求数据->", requestData);
        //设置为请求数据为json类型
        ajax.setContentType('application/json');
        ajax.setData(requestData);
        ajax.start();
    }
}




$(function () {
    var defaultColumns = GoodsOverflow.initGoodsOverflowColumn();
    var table = new BSTable(GoodsOverflow.id, "", defaultColumns);
    table.setUniqueId("goodsId");
    table.setPaginationType("client");
    GoodsOverflow.table = table.init();
});
